const http = require('http')
const path = require('path')
const url  = require('url')
const fs   = require('fs')


http.createServer(
    (req,res)=>{
        let reqUrl = url.parse(req.url).pathname;
        
        // 文件结尾
        let extname = path.extname(reqUrl)
        console.log(extname)

        if(reqUrl === '/index.html'){
            let data = fs.readFileSync(path.join(__dirname,'./dist/index.html'),'utf8')
            res.writeHead(200,{'Content-Type':'text/html;charset=UTF8'});
            res.end(data)
            return
        }
        if(reqUrl.includes('/static')){

            let data = fs.readFileSync(path.join(__dirname,'./dist',reqUrl),'utf8')
            let mime = getMIME(extname)
            if(mime === undefined){
                mime = 'text/html;charset=UTF8'
            }
            res.writeHead(200,{'Content-Type':mime});
            res.end(data)
            return
        }

        if(reqUrl === '/kfc/list'){
            let data = fs.readFileSync(path.join(__dirname,'./kfc/kfclist.json'),'utf8')
            res.writeHead(200,{'Content-Type':'text/html;charset=UTF8'});
            res.end(data)
            return
        }

        if(reqUrl === '/roome/lately'){
            let data = fs.readFileSync(path.join(__dirname,'./roome/lately.json'),'utf8')
            res.writeHead(200,{'Content-Type':'text/html;charset=UTF8'});
            res.end(data)
            return
        }

        if(reqUrl === '/roome/like'){
            let data = fs.readFileSync(path.join(__dirname,'./roome/like.json'),'utf8')
            res.writeHead(200,{'Content-Type':'text/html;charset=UTF8'});
            res.end(data)
            return
        }

        res.end('没有对应数据')
    }
).listen(7001,()=>console.log('服务开启'))



let getMIME = extname =>{
    switch(extname){
        case ".html" :
            return "text/html;charset=UTF8";
            break;
        case ".jpg" :
            return "image/jpg";
            break
        case ".png" :
            return "image/png";
            break
        case ".css" :
            return "text/css";
            break
        case ".js" :
            return "application/javascript";
            break
        case ".json" :
            return "application/json";
            break
    }
}